package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    private IEmployeeService employeeService;
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @Autowired
    public void setEmployeeService(IEmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    //查询请求
    @RequestMapping("/list")
    @RequiredPermission(name = "员工列表", expression = "employee.list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Department> departments = departmentService.selectAll();
        //查询所有部门往模型存入数据,让员工列表显示出来让用户选择
        model.addAttribute("departments", departments);
        return "employee/list";
    }

    //删除请求
    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除", expression = "employee.delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        //删除以后重定向到查询页面
        return "redirect:/employee/list";
    }

   /* //删除请求
    @RequestMapping(value = "employees", method = RequestMethod.DELETE)
    @RequiredPermission(name = "员工删除", expression = "employee.delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        //删除以后重定向到查询页面
        return "redirect:/employee/list";
    }*/

    //去增加或者去修改页面
    @RequestMapping("/input")
    @RequiredPermission(name = "员工修改或者增加", expression = "employee.saveOrUpdate")
    public String input(Long id, Model model) {
        List<Department> departments = departmentService.selectAll();
        //查询所有部门往模型存入数据,让员工列表显示出来让用户选择
        model.addAttribute("departments", departments);
        //把所有的角色数据存储起来
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles", roles);
        if (id != null) {
            Employee employee = employeeService.selectOne(id);
            model.addAttribute("employee", employee);
            model.addAttribute("selfRoles", roleService.selectByEmpId(id));
        }
        return "/employee/input";
    }

    //新增或者修改
    @RequestMapping("/saveOrUpdate")
    public String update(Employee employee, Long[] ids) {
        if (employee.getId() == null) {
            //如果id等于空那就新增
            employeeService.insert(employee, ids);
        } else {
            //如果id不为空那就去修改
            employeeService.update(employee, ids);
        }
        //修改或者插入完以后使用重定向到查询页面
        return "redirect:/employee/list";
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
       // String fileName = new String("员工信息".getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-Disposition","attachment;filename=workbook.xls");
     employeeService.exportXls(response.getOutputStream());
    }


    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }
}
